*******************************************************************************
* > Loop over subgroups
*******************************************************************************

foreach subgroup in All Democrat Republican {

*******************************************************************************
* >> Loop over datasets
*******************************************************************************

foreach dataset in gallup gss {

*******************************************************************************
* Load data
*******************************************************************************

use dataset_`dataset', clear
	
*******************************************************************************
* Keep the subgroup
*******************************************************************************

keep if subgroup == "`subgroup'"

*******************************************************************************
* Set params based on data set
*******************************************************************************

if "`dataset'" == "gallup" {
	rename conf Y
	local mytitle2 "Confidence (Gallup)"
	local treated_target "police"
	local yaxis -.3(.2).5
	local xaxis 1994(5)2014
}
if "`dataset'" == "gss" {
	rename nat Y
	local mytitle2 "Spending (GSS)"
	local treated_target "crimy"
	local yaxis -.6(.2).6
	local xaxis 1984(10)2014
}

*******************************************************************************
* >>> Loop over placebo years
*******************************************************************************

forvalues placeboyear = 2002(2)2014 {

preserve

*******************************************************************************
* Make lags
*******************************************************************************

if `placeboyear' < 2014 keep if year < 2014

su year
local mymin = r(min)

local lags
levelsof year if year < `placeboyear', local(levels)

foreach i of local levels {
	egen Y`i'm = mean(Y) if year == `i', by(target)
	egen Y`i' = mean(Y`i'm), by(target)
	drop Y`i'm
	local lags `lags' Y`i'
}

*******************************************************************************
* Set panel and get the treated ID
*******************************************************************************

encode target, gen(id)
xtset id year
su id if target == "`treated_target'"

*******************************************************************************
* Synth
*******************************************************************************

synth Y `lags', trunit(`r(mean)') trperiod(`placeboyear') keep(tmp, replace)
use tmp, clear
gen placeboyear = `placeboyear'
if `placeboyear' > 2002 append using tmp0
save tmp0, replace

*******************************************************************************
* >>> Close placebo year loop
*******************************************************************************

restore

}

*******************************************************************************
* Graph
*******************************************************************************

use tmp0, clear
erase tmp0.dta
gen d = _Y_treated - _Y_synthetic
gen _time2 = _time - placeboyear

levelsof placeboyear, local(levels)
foreach l of local levels {
	local p (line d _time2 if placeboyear == `l', col(gray))
	local placebos `placebos' `p'
}

#delimit;

gr tw
	`placebos'
	(line d _time2 if placeboyear==2014, col(black) lwid(thick))
	,
		legend(off)
		name(g`dataset', replace)
		yline(0, lpat(shortdash))
		xline(0, lpat(dash))
		plotregion(style(none))
		ylab(`yaxis', angle(horiz))
		ytitle("")
		xtitle("")
		//xlab(`xaxis')			
		title("`mytitle2'")		
		;

#delimit cr

local mytitle `subgroup'
if "`subgroup'" != "All" local mytitle = "`subgroup's" 

*******************************************************************************
* >> Close dataset loop
*******************************************************************************

}

*******************************************************************************
* Combine plots for each dataset
*******************************************************************************

local mytitle `subgroup'
if "`subgroup'" != "All" local mytitle = "`subgroup's" 

gr combine ggallup ggss, name(G`subgroup', replace) title(`mytitle')

*******************************************************************************
* > Close subgroup loop
*******************************************************************************

}

*******************************************************************************
* Combine plots for each subgroup
*******************************************************************************

gr combine GAll GDemocrat GRepublican, rows(3) ysize(4) xsize(3) ///
	imargin(zero)

*******************************************************************************
* Save
*******************************************************************************

gr export "figure_sc_placebos_time_all.pdf", replace

*******************************************************************************
* End
*******************************************************************************